package cn.iocoder.yudao.module.system.job.flow;

import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.framework.tenant.core.job.TenantJob;
import cn.iocoder.yudao.module.system.service.config.InfraConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;

@Component
@Slf4j
public class DayFlowJob implements JobHandler {

    @Resource
    private InfraConfigService infraConfigService;

    @Override
    @TenantIgnore
    public String execute(String param) throws Exception {
        String dayFlow = infraConfigService.getConfigByKey("DAY_FLOW");
        int updateNum=0;
        int num =0;
        log.info("定时流量增加执行开始");
        if( StringUtils.isNotBlank(dayFlow) && StringUtils.equals("1",dayFlow)){
            //日增阈值-最高值
            String dayFlowMax = infraConfigService.getConfigByKey("DAY_FLOW_MAX","5");
            //日增阈值-最低值
            String dayFlowMin = infraConfigService.getConfigByKey("DAY_FLOW_MIN","1");
            if(StringUtils.isNotBlank(param)){
                num=Integer.parseInt(param);
            }else{
                num = mockIntBetween(Integer.parseInt(dayFlowMin), Integer.parseInt(dayFlowMax));
            }
            log.info("定时流量增加:"+DateUtils.format(new Date())+",num:"+num);
            updateNum = infraConfigService.updateJobDayFlow(num);
        }
        log.info("定时流量增加执行结束");
        return String.format("更新职位日增流量个数 %s 个", updateNum);
    }

    /**
     * @Description: 生成int随机数
     * @Author: wsp
     * @Date: 2021/9/24 17:56
     **/
    public static int mockIntBetween(int begin, int end) {
        int between = end - begin;
        double random = Math.random();
        int randomBetween = new Double(random * between).intValue();
        int result = begin + randomBetween;
        return result;
    }
}
